package main

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"log"
	"palms/controller"
	"palms/global"
	"palms/launch"
	"palms/repository"
	"palms/service"
)

func main() {
	global.Engine = launch.Xorm()
	global.Scheduler = launch.Gocron()

	app := iris.New()
	app.Logger().SetLevel("debug")
	mvc.Configure(app.Party("/robots"), robots)
	mvc.Configure(app.Party("/tasks"), tasks)

	err := app.Run(
		iris.Addr("0.0.0.0:8080"),
		iris.WithoutServerError(iris.ErrServerClosed),
		iris.WithOptimizations,
	)
	if err != nil {
		log.Fatal("服务启动失败")
	}
}

func robots(app *mvc.Application) {
	repo := repository.NewRobotRepository(global.Engine)
	srv := service.NewRobotService(repo)
	app.Register(srv)
	app.Handle(new(controller.RobotController))
}

func tasks(app *mvc.Application) {
	repo := repository.NewTaskRepository(global.Engine)
	srv := service.NewTaskService(repo)
	app.Register(srv)
	app.Handle(new(controller.TaskController))
}
